package com.app.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.Element;

import sun.net.www.content.text.plain;

import com.app.util.WeatherAPIHelper;
import com.app.util.XmlHelper;

public class APIMaster extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public APIMaster() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WeatherAPIHelper apiHelper = new WeatherAPIHelper();
		XmlHelper xmlHelper = new XmlHelper();
		TreeMap<String, String> re = xmlHelper.read(request);
		String op = re.get("op");
		if (op != null && op.equals("weather")) {
			String cityName = re.get("city_name");
			String when = re.get("when");
			String length = re.get("length");
			if (cityName != null) {
				if (when != null) {
					if (length != null) {
						@SuppressWarnings("deprecation")
						Date date = new Date(when);
						Document document = apiHelper.getInfoByCityName(
								cityName, date, length);
						out(document.asXML(), request, response);
					}
				}
			}
		}
	}

	private void out(String asXML, HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter print = null;
		try {
			print = response.getWriter();
			response.setContentType("text/xml; charset=utf-8");
			print.write(asXML);
			print.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (print != null)
				print.close();
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
